package com.aierjun.ani.net.OkGo;

import android.os.Environment;

import com.lzy.okgo.*;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okserver.OkDownload;
import com.lzy.okserver.download.DownloadListener;
import com.lzy.okserver.download.DownloadTask;

import java.io.File;

/**
 * Created by Ani_aierJun on 2017/10/19.
 */

public class DownloadUtils {
    private static OkDownload okDownload;
    private static DownloadTask task;

    public static OkDownload init(String folder) {
        okDownload = OkDownload.getInstance();
        if (folder == null || folder.equals("")) {
            String path = Environment.getExternalStorageDirectory().getPath() + "/Ani/";
            okDownload.setFolder(path);
        } else {
            okDownload.setFolder(folder);
        }
        return okDownload;
    }

    public static DownloadTask download(String url, HttpHeaders headers, HttpParams httpParams, String tag, DownloadListener downloadListener) {
        init(null);
        GetRequest<File> request = com.lzy.okgo.OkGo.<File>get(url)
                .headers(headers)
                .params(httpParams);
        task = OkDownload.request(tag, request)
//                .extra1()
                .save()
                .register(downloadListener);
        return task;
    }

    public static void startAll() {
        okDownload.startAll();
    }

    public static void pauseAll() {
        okDownload.pauseAll();
    }

    public static void removeAll() {
        okDownload.removeAll();
    }

    public static void removeAllFile() {
        okDownload.removeAll(true);
    }

    public static void start(){
        task.start();
    }

    public static void pause(){
        task.pause();
    }

    public static void restart(){
        task.restart();
    }

    public static void remove(){
        task.remove();
    }

    public static void removeFile(){
        task.remove(true);
    }


}
